﻿using System.IO;
using Net.System;
using WebSocketSharp;

namespace Net.Server
{
    /// <summary>
    /// http客户端对象
    /// </summary>
    public class HttpPlayer : ClientPeerBase
    {
        /// <summary>
        /// webSocket套接字
        /// </summary>
        public WebSocket WSClient { get; set; }

        //private void WSRevdHandler(Player client, string text)
        //{
        //    try
        //    {
        //        var message = JsonConvert.DeserializeObject<MessageModel>(text);
        //        var model = RpcUnitFactory.Create(cmd: message.cmd, kernel: true, protocol: message.func.CRCU32(), pars: message.GetPars());
        //        DataHandler(client, model, null);
        //    }
        //    catch (Exception ex)
        //    {
        //        Debug.LogError($"[{client}]json解析出错:" + ex);
        //        var model = new MessageModel(0, "error", new object[] { ex.Message });
        //        var jsonStr = JsonConvert.SerializeObject(model);
        //        client.WSClient.Send(jsonStr);
        //    }
        //}

        protected override void SendByteData(ISegment buffer)
        {
            trafficStatistics.SentPacketsPerSecond++;
            trafficStatistics.SentBytesPerSecond += buffer.Count;
            WSClient.Send(new MemoryStream(buffer.Buffer, buffer.Offset, buffer.Count, true, true), buffer.Count);
        }

        public override void Dispose()
        {
            base.Dispose();
            WSClient?.Close();
        }
    }
}